package com.winning.sx.microframework.controller.sysmanage;

import com.winning.sx.microframework.dao.sysmanage.ModelManageDao;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.ModelManageModel;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxResponseMessage;
import com.winning.sx.microframework.common.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MJ on 2016-10-17.
 */
@Controller
@RequestMapping(value = "modelmanage")
public class ModelManageController {
    @Autowired
    private ModelManageDao modelManageDao;

    @RequestMapping(value = {"/", ""})
    public ModelAndView modelindex() {
        return new ModelAndView("manage/modelmanage");
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public AjaxDataResponseMessage save(ModelManageModel model) {
        AjaxDataResponseMessage ajaxResponseMessage = new AjaxDataResponseMessage();
        try {
            if (StringUtils.isEmpty(model.getUpdateType()))
                throw new CustomException(-2001, "更新类别不能为空!");

            Map<String, Object> map = BaseUtil.beanToMap(model);

            if ("NEW.TOP".equals(model.getUpdateType())) {
                ModelManageModel newModel = this.modelManageDao.newModel(model);
                ajaxResponseMessage.setData(newModel);
            } else if ("NEW.CHILD".equals(model.getUpdateType())) {
                model.setcFatherModelID(model.getcModelID());
                ModelManageModel newModel = this.modelManageDao.newModel(model);
                ajaxResponseMessage.setData(newModel);
            } else if ("UPDATE".equals(model.getUpdateType())) {
                this.modelManageDao.updateModel(model);
                ajaxResponseMessage.setData(model);
            } else
                throw new CustomException(-2001, "更新类别不能识别!");
            ajaxResponseMessage.setSuccess(true);
        } catch (Exception e) {
            this.handelException(ajaxResponseMessage, e);
        }
        return ajaxResponseMessage;
    }

    @RequestMapping(value = {"menutree"}, method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView menutree() {
        ModelAndView mv = new ModelAndView("manage/menu-tree-data");
        try {
            List<ModelManageModel> models =
                    this.modelManageDao.queryModels();

            mv.addObject("menus", models);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mv;
    }

    @RequestMapping(value = {"menutree/{modelid}"}, method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView menutreeByXh(@PathVariable("modelid") String modelid) {

        ModelAndView mv = new ModelAndView("manage/menu-tree-data");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("modelid", modelid);
        try {
            List<ModelManageModel> menus = this.modelManageDao.queryModelsByID(map);
            mv.addObject("menus", menus);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mv;
    }

    @ResponseBody
    @RequestMapping(value = {"menudata/{modelid}"}, method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxDataResponseMessage menudata(@PathVariable("modelid") String modelid) {

        AjaxDataResponseMessage ajaxResponseMessage = new AjaxDataResponseMessage();

        try {
            ModelManageModel model = this.modelManageDao.loadModel(modelid);
            ajaxResponseMessage.setData(model);
            ajaxResponseMessage.setSuccess(true);
        } catch (Exception e) {
            this.handelException(ajaxResponseMessage, e);
        }

        return ajaxResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"delete/{modelid}"}, method = {RequestMethod.DELETE})
    public AjaxResponseMessage deleteWebMenu(@PathVariable("modelid") String modelid) {

        AjaxResponseMessage ajaxResponseMessage = new AjaxResponseMessage();

        try {
            this.modelManageDao.deleteModel(modelid);

            ajaxResponseMessage.setSuccess(true);
        } catch (Exception e) {
            this.handelException(ajaxResponseMessage, e);
        }

        return ajaxResponseMessage;
    }

    private void handelException(AjaxResponseMessage ajaxResponseMessage, Exception e) {
        ajaxResponseMessage.setSuccess(false);
        if (e instanceof CustomException)
            ajaxResponseMessage.setErrorMsg(e.getMessage());
        else
            ajaxResponseMessage.setErrorMsg("发生内部错误:" + e.getMessage());
    }
}
